﻿using System;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;

namespace KZ.Lottery
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtUsername.Text)) { throw new BusinessLogicException(KZ_Message.BLM_ERR_USERNAME_REQUIRED); }
                if (String.IsNullOrEmpty(txtPassword.Text)) { throw new BusinessLogicException(KZ_Message.BLM_ERR_PASSWORD_REQUIRED); }

                Setting setting = SettingBLL.GetSetting(KZ_Resource.Setting_Username);
                if (txtUsername.Text.ToLower() != setting.SettingValue.ToLower()) { throw new BusinessLogicException(KZ_Message.BLM_ER_USERNAME_INCORRECT); }

                setting = SettingBLL.GetSetting(KZ_Resource.Setting_Password);
                if (txtPassword.Text != setting.SettingValue) { throw new BusinessLogicException(KZ_Message.BLM_ERR_PASSWORD_INCORRECT); }

                DialogResult = DialogResult.Yes;
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (BusinessLogicException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("btnLogin_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnLogin_Click(btnLogin, null);
            }
        }
    }
}